package 小测试;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

/**
 * Created by Administrator on 2017/8/7.
 */
public class Map遍历 {
    public static void main(String[] args) {
        HashMap<Integer, String> map = new HashMap<>();
        map.put(1,"张三");
        map.put(2,"李四");
        map.put(3,"王五");
        //遍历方式1：通过keySet
        Set<Integer> integers = map.keySet();
        for (Integer i :
                integers) {
            System.out.println(i+":"+map.get(i));
        }
        //遍历方式2：通过Map.Entry<K,V>
        Set<Map.Entry<Integer, String>> entries = map.entrySet();
        for (Map.Entry<Integer,String> entry:
             entries) {
            System.out.println(entry.getKey()+":"+entry.getValue());
        }
    }
}
